﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BlogManager.Core.BaseArchitecture;

namespace BlogManager.Application
{
    public partial class BlogPosts : AppDisplayBase
    {
        private int? siteId = null;
        private int? postId = null;

        public override void PageLoad()
        {
            if (!String.IsNullOrEmpty(Request.QueryString["siteId"]))
                siteId = Convert.ToInt32(Request.QueryString["siteId"]);

            if (!String.IsNullOrEmpty(Request.QueryString["postId"]))
                postId = Convert.ToInt32(Request.QueryString["postId"]);

            //They want to delete....
            if (postId != null && siteId != null)
                Delete();
        }

        public override void PageDataLoad()
        {
            Rebind();
        }

        public override void PagePostBackLoad()
        {
            Rebind();
        }

        protected void Rebind()
        {
            if (siteId!=null)
            {
                var posts = Data.Entities.Blog_Post.All().Where(x => x.SiteId == siteId);

                GridViewForPosts.DataSource = posts;
                GridViewForPosts.DataBind();
            }
        }

        protected override void Delete()
        {
            Data.Entities.Blog_Post.Delete(x => x.Id == postId && x.SiteId == siteId);
        }

        public string parseContent(string content)
        {
            if (content.Length > 90)
                return content.Substring(0,90)+"...";

            return content;
        }
    }
}